package com.ymatou.iapi.rmq.dispatcher.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.rmq.dispatcher.parameter.DispatchBean;
import com.ymatou.iapi.rmq.dispatcher.service.DispatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.mongo.JMQMessageBean;
import com.ymttest.database.mongo.JMQMessageCompensateBean;
import com.ymttest.database.mongo.JMQMessageCompensateWapper;
import com.ymttest.database.mongo.JMQMessageDetailBean;
import com.ymttest.database.mongo.JMQMessageDetailWapper;
import com.ymttest.database.mongo.JMQMessageWapper;
import com.ymttest.database.mongo.MQMessageBean;
import com.ymttest.database.mongo.MQMessageCompensateBean;
import com.ymttest.database.mongo.MQMessageCompensateWapper;
import com.ymttest.database.mongo.MQMessageStatusBean;
import com.ymttest.database.mongo.MQMessageStatusWapper;
import com.ymttest.database.mongo.MQMessageWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 分发消息用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_dispatch {
	private static DispatchBean dispatchBean;
	private static DispatchCall dispatchCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分发消息用例");
	}

	@Before
	public void caseUp() {
		dispatchBean = new DispatchBean();
		dispatchCall = new DispatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(dispatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_001() {
		Logger.start(true, "分发消息-接收成功");
		try {
						String msguniqueid = TestDataManager.getRandomNum(10);
						String body = TestDataManager.randomStr(20);
						String appName = "testdashen";
						String codeName = "publishok";
						// String codeName = "miaosi";

						dispatchBean.setAppId(appName);
						dispatchBean.setCode(codeName);
						dispatchBean.setIp("127.0.0.1");
						dispatchBean.setMsgUniqueId(msguniqueid);
						dispatchBean.setBody(body);

						dispatchCall.setData(dispatchBean);
						dispatchCall.callService();

						Logger.verifyEquals("true", dispatchCall.getSuccess(), "验证Success");
						Thread.sleep(1000);
						
						// 验证JMQ_V2_Message库
						JMQMessageBean messageBean = new JMQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

						Date nowTime = new Date();
						Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

						// 验证Message表
						Logger.verifyEquals(dispatchBean.getAppId(), messageBean.getAppId(), "验证数据库Aid");
						Logger.verifyEquals(dispatchBean.getCode(), messageBean.getQueueCode(), "验证数据库Code");
						Logger.verifyEquals(dispatchBean.getBody(), messageBean.getBody(), "验证数据库Body");			
						Logger.verifyEquals(dispatchBean.getIp(), messageBean.getClientIp(), "验证数据库ClientIp");
						Logger.verifyEquals(true,messageBean.getRecvIp().contains("137"), "验证数据库RecvIp");
						Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
						
						// 验证返回UUID与数据库message的id一致
						Logger.verifyEquals(messageBean.get_id(), dispatchCall.getUUID(), "验证数据库uuid");
						
						// 验证MessageDetail表
						JMQMessageDetailBean messageDetailBean = new JMQMessageDetailWapper().getMessageDetailByMid(appName, codeName, msguniqueid);

						int flag = 0;

						while (messageDetailBean == null || messageDetailBean.getLastResp()==null)  {
							Thread.sleep(200);
							flag++;
							 messageDetailBean = new JMQMessageDetailWapper().getMessageDetailByMid(appName, codeName, msguniqueid);
						}

						Logger.verifyEquals(messageBean.get_id(), messageDetailBean.getMsgId(), "验证数据库BusIp");
						Logger.verifyEquals(dispatchBean.getAppId(), messageDetailBean.getAppId(), "验证数据库Aid");
						Logger.verifyEquals(dispatchBean.getCode(), messageDetailBean.getQueueCode(), "验证数据库Code");
						Logger.verifyEquals("testdashen_publishok_c0", messageDetailBean.getConsumerId(), "验证数据库consumerId");
						Logger.verifyEquals(1, messageDetailBean.getLastFrom(), "验证数据库lastFrom");
						Logger.verifyEquals(true, messageDetailBean.getLastResp().contains("200, ok"), "验证数据库callNum");
						Logger.verifyEquals(1, messageDetailBean.getStatus(), "验证数据库status:0初始化，1成功，2失败，3补单中");					
						Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
						Logger.verifyEquals(true, messageDetailBean.getDealIp().contains("137"), "验证数据库DealIp");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_002() {
		Logger.start(true, "分发消息-接收失败");
		try {

			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail";
			// String codeName = "miaosi";

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("true", dispatchCall.getSuccess(), "验证Success");
			Thread.sleep(1000);
			
			// 验证JMQ_V2_Message库
			JMQMessageBean messageBean = new JMQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(dispatchBean.getAppId(), messageBean.getAppId(), "验证数据库Aid");
			Logger.verifyEquals(dispatchBean.getCode(), messageBean.getQueueCode(), "验证数据库Code");
			Logger.verifyEquals(dispatchBean.getBody(), messageBean.getBody(), "验证数据库Body");			
			Logger.verifyEquals(dispatchBean.getIp(), messageBean.getClientIp(), "验证数据库ClientIp");
			Logger.verifyEquals(true,messageBean.getRecvIp().contains("137"), "验证数据库RecvIp");
			Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
			
			// 验证返回UUID与数据库message的id一致
			Logger.verifyEquals(messageBean.get_id(), dispatchCall.getUUID(), "验证数据库uuid");
			
			// 验证MessageDetail表
			JMQMessageDetailBean messageDetailBean = new JMQMessageDetailWapper().getMessageDetailByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageDetailBean == null || messageDetailBean.getStatus()==0|| messageDetailBean.getStatus()==2) {
				Thread.sleep(2000);
				flag++;
				messageDetailBean = new JMQMessageDetailWapper().getMessageDetailByMid(appName, codeName, msguniqueid);
			}

			Logger.verifyEquals(messageBean.get_id(), messageDetailBean.getMsgId(), "验证数据库BusIp");
			Logger.verifyEquals(dispatchBean.getAppId(), messageDetailBean.getAppId(), "验证数据库Aid");
			Logger.verifyEquals(dispatchBean.getCode(), messageDetailBean.getQueueCode(), "验证数据库Code");
			Logger.verifyEquals("testdashen_publishfail_c0", messageDetailBean.getConsumerId(), "验证数据库consumerId");
			Logger.verifyEquals(1, messageDetailBean.getLastFrom(), "验证数据库lastFrom");
			Logger.verifyEquals(true, messageDetailBean.getLastResp().contains("200, fail"), "验证数据库callNum");
			Logger.verifyEquals(3, messageDetailBean.getStatus(), "验证数据库status:0初始化，1成功，2失败，3补单中");					
			Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
			Logger.verifyEquals(true, messageDetailBean.getDealIp().contains("137"), "验证数据库DealIp");
	
			// 验证补单表
			JMQMessageCompensateBean compensateBean = new JMQMessageCompensateWapper().getCompensateByMid(dispatchBean.getAppId(),dispatchBean.getCode(), messageBean.get_id().toString());

			while (compensateBean == null || compensateBean.getStatus()==0) {
				Thread.sleep(2000);
				flag++;
				compensateBean = new JMQMessageCompensateWapper().getCompensateByMid(dispatchBean.getAppId(),dispatchBean.getCode(), messageBean.get_id().toString());
			}
			
			Logger.verifyEquals(compensateBean.getMsgId()+"_"+compensateBean.getConsumerId(), compensateBean.get_id(), "验证数据库_id");
			Logger.verifyEquals(dispatchBean.getAppId(), compensateBean.getAppId(), "验证数据库AppId");
			Logger.verifyEquals(dispatchBean.getCode(), compensateBean.getQueueCode(), "验证数据库QueueCode");
			Logger.verifyEquals(messageDetailBean.getConsumerId(), compensateBean.getConsumerId(), "验证数据库consumerId");
			Logger.verifyEquals(dispatchBean.getMsgUniqueId(), compensateBean.getBizId(), "验证数据库BizId");
			Logger.verifyEquals(dispatchBean.getBody(), compensateBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(1, compensateBean.getSource(), "验证数据库Source:1:分发进补单;2;检测进补单");
			Logger.verifyEquals(0, compensateBean.getStatus(), "验证数据库Status:0初始化，1成功，2失败，3补单中");
			Logger.verifyEquals(true, compensateBean.getCompensateNum()>0, "验证数据库CompensateNum");
			Logger.verifyEquals(2, compensateBean.getLastFrom(), "验证数据库LastFrom");
			Logger.verifyEquals("fail", compensateBean.getLastResp(), "验证数据库LastResp");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_003() {
		Logger.start(false, "分发消息-AppId空");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail";
			// String codeName = "miaosi";

			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("app not empty", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_004() {
		Logger.start(false, "分发消息-Code空");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail";
			// String codeName = "miaosi";

			dispatchBean.setAppId(appName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("code not empty", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_005() {
		Logger.start(false, "分发消息-mid空");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail";
			// String codeName = "miaosi";

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
//			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("messageId not empty", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_007() {
		Logger.start(false, "分发消息-body可空");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail";

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
//			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("body not null", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_008() {
		Logger.start(false, "分发消息-AppId不存在");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen111";
			String codeName = "publishfail";
			
			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("FAIL", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("appId:[testdashen111],queueCode:[publishfail] not exist.", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_009() {
		Logger.start(false, "分发消息-Code不存在");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testdashen";
			String codeName = "publishfail111";
			
			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("false", dispatchCall.getSuccess(), "验证Code");
			Logger.verifyEquals("FAIL", dispatchCall.getErrorCode(), "验证Msg");
			Logger.verifyEquals("appId:[testdashen],queueCode:[publishfail111] not exist.", dispatchCall.getErrorMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_013() {
		Logger.start(true, "分发消息-回调超时进入补单库");
		try {
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testbamei";
			String codeName = "zaiyouqusi";
			// String codeName = "miaosi";

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setIp("127.0.0.1");
			dispatchBean.setMsgUniqueId(msguniqueid);
			dispatchBean.setBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("true", dispatchCall.getSuccess(), "验证Success");
			
			// 验证JMQ_V2_Message库
			JMQMessageBean messageBean = new JMQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			Thread.sleep(18000);
			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(dispatchBean.getAppId(), messageBean.getAppId(), "验证数据库Aid");
			Logger.verifyEquals(dispatchBean.getCode(), messageBean.getQueueCode(), "验证数据库Code");
			Logger.verifyEquals( dispatchBean.getBody(), messageBean.getBody(), "验证数据库Body");			
			Logger.verifyEquals(dispatchBean.getIp(), messageBean.getClientIp(), "验证数据库ClientIp");
			Logger.verifyEquals(true,messageBean.getRecvIp().contains("137"), "验证数据库RecvIp");
			Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
			
			// 验证返回UUID与数据库message的id一致
			Logger.verifyEquals(messageBean.get_id(), dispatchCall.getUUID(), "验证数据库uuid");
			
			// 验证MessageDetail表
			JMQMessageDetailBean messageDetailBean = new JMQMessageDetailWapper().getMessageDetailByMid(appName, codeName, msguniqueid);

			Logger.verifyEquals(messageBean.get_id(), messageDetailBean.getMsgId(), "验证数据库BusIp");
			Logger.verifyEquals(dispatchBean.getAppId(), messageDetailBean.getAppId(), "验证数据库Aid");
			Logger.verifyEquals(dispatchBean.getCode(), messageDetailBean.getQueueCode(), "验证数据库Code");
			Logger.verifyEquals("testbamei_zaiyouqusi_c1", messageDetailBean.getConsumerId(), "验证数据库consumerId");
			Logger.verifyEquals(1, messageDetailBean.getLastFrom(), "验证数据库lastFrom");
			Logger.verifyEquals(true, messageDetailBean.getLastResp().contains("java.net.SocketTimeoutException"), "验证数据库callNum");
			Logger.verifyEquals(3, messageDetailBean.getStatus(), "验证数据库status:0初始化，1成功，2失败，3补单中");					
			Logger.verifyEquals(sdf.format(date), messageBean.getCreateTime().toString().subSequence(7, 20), "验证数据库Ctime");
			Logger.verifyEquals(true, messageDetailBean.getDealIp().contains("137"), "验证数据库DealIp");
	
			// 验证补单表
			JMQMessageCompensateBean compensateBean = new JMQMessageCompensateWapper().getCompensateByMid(dispatchBean.getAppId(),dispatchBean.getCode(), messageBean.get_id().toString());

			Logger.verifyEquals(compensateBean.getMsgId()+"_"+compensateBean.getConsumerId(), compensateBean.get_id(), "验证数据库_id");
			Logger.verifyEquals(dispatchBean.getAppId(), compensateBean.getAppId(), "验证数据库AppId");
			Logger.verifyEquals(dispatchBean.getCode(), compensateBean.getQueueCode(), "验证数据库QueueCode");
			Logger.verifyEquals(messageDetailBean.getConsumerId(), compensateBean.getConsumerId(), "验证数据库consumerId");
			Logger.verifyEquals(dispatchBean.getMsgUniqueId(), compensateBean.getBizId(), "验证数据库BizId");
			Logger.verifyEquals(dispatchBean.getBody(), compensateBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(1, compensateBean.getSource(), "验证数据库Source:1:分发进补单;2;检测进补单");
			Logger.verifyEquals(0, compensateBean.getStatus(), "验证数据库Status:0补单中，1成功，2失败");
			Logger.verifyEquals(true, compensateBean.getCompensateNum()>0, "验证数据库CompensateNum");
			Logger.verifyEquals(2, compensateBean.getLastFrom(), "验证数据库LastFrom");
			Logger.verifyEquals(true, compensateBean.getLastResp().contains("SocketTimeoutException"), "验证数据库LastResp");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}